//
//  UserAccountViewModel.swift
//  01sinablog
//
//  Created by ZhangZiang on 15/12/14.
//  Copyright © 2015年 ZhangZiang. All rights reserved.
//

import UIKit
import AFNetworking

class UserAccountViewModel: NSObject {

    var isUserLogin : Bool?
    
    var userAccount: UserAccount?
    
    override init() {
        super.init()
        
        userAccount = UserAccount.loadAccount()
        isLogined()
    }
    //判端是否本地有用户信息
    private func isLogined()
    {
        if userAccount?.access_token != nil
        {
            isUserLogin = true
        }
        else
        {
            isUserLogin = false
        }
        
        
    }
    
    //去获取令牌,因为HTTP的请求方式是POST,不能像GET那样用浏览器加载,所以用AFN
    func loadToken(code : String, finish:(isSuccess : Bool) -> ())
    {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        //        let urlString = firstStr + "?client_id=" + client_id + "&client_secret=" + client_secret + "&grant_type=authorization_code&code=" + code + "&redirect_uri=" + redirect_uri
        //        print(urlString)
        //        let url = NSURL(string: "https://api.weibo.com/oauth2/access_token")
        let parameters = Dictionary(dictionaryLiteral: ("client_id", client_id),("client_secret",client_secret),("grant_type","authorization_code"),("code",code),("redirect_uri",redirect_uri))
        
        NetworkTool.sharedNetworkTool.requestJSONDict(.POST, parameters: parameters, urlString: urlString) { (dict, error) -> () in
            
            if error != nil {
                finish(isSuccess: false)
                return
            }
  
            let account = UserAccount().userAccountWithDict(dict!)
            self.loadUserInfo(account, finish: finish)

        }

//        
//        let AFN = AFHTTPSessionManager()
//        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
//        AFN.POST(urlString, parameters: parameters, success: { (_, result) -> Void in
//            //            print(result)
//            if let dict = result as? [String : AnyObject]
//            {
//                //在这里获取了数据,需要来个模型
//                let account = UserAccount().userAccountWithDict(dict)
//                self.loadUserInfo(account, finish: finish)
//            }
//            
//            
//            }) { (_, error) -> Void in
//                print(error)
//        }
    }
    //去获得用户的信息
    private func loadUserInfo(account : UserAccount, finish:(isSuccess : Bool) -> ())
    {
        let urlString = "https://api.weibo.com/2/users/show.json"
        
//        let AFN = AFHTTPSessionManager()
        
        if let access_token = account.access_token, uid = account.uid
        {
            let parameters : [String : String] = ["access_token":access_token, "uid":uid]
            
            NetworkTool.sharedNetworkTool.requestJSONDict(.GET, parameters: parameters, urlString: urlString, finish: { (dict, error) -> () in
                if error != nil {
                    finish(isSuccess: false)
                    return
                }

                account.name = dict!["name"] as? String
                account.avatar_large = dict!["avatar_large"] as? String
                //归档得到的用户数据
                account.saveAccount()
                
                self.userAccount = account
                
                finish(isSuccess: true)
            })
            
//            AFN.GET(urlString, parameters: parameters, success: { (_, result) -> Void in
//                //取得result,用户信息
//                print(result)
//                if let dict : [String : AnyObject] = result as? [String : AnyObject]
//                {
//                    account.name = dict["name"] as? String
//                    account.avatar_large = dict["avatar_large"] as? String
//                    //归档得到的用户数据
//                    account.saveAccount()
//                    
//                    self.userAccount = account
//                    
//                    finish(isSuccess: true)
//                }
//                
//                }, failure: { (_, error) -> Void in
//                    print(error)
//                    
//                    finish(isSuccess: false)
//            })
            
        }
        
    }

}
